package sample;

import chloroform.oauth.OAuthAccessTokenAndSecret;
import chloroform.oauth.OAuthAccessTokenAndSecret$;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.authn.oauth.OAuthRsaSha1Signer;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.ServiceException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URL;
import java.security.PrivateKey;

/**
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: 3.3.11
 * Time: 14:59
 * To change this template use File | Settings | File Templates.
 */
public class GDocBrowseServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            DocsService client = new DocsService("yourCompany-YourAppName-v1");
            OAuthAccessTokenAndSecret access = OAuthAccessTokenAndSecret$.MODULE$.get(req.getSession());
            if (access == null) {
                throw new ServletException("Not authorized (OAuth)");
            }
            //client.setOAuthCredentials(oauthParameters, new OAuthRsaSha1Signer(privKey));
            access.setCredentials(client);

            URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full");
            DocumentListFeed resultFeed = client.getFeed(feedUrl, DocumentListFeed.class);
            PrintWriter writer = resp.getWriter();
            for (DocumentListEntry entry : resultFeed.getEntries()) {
                writer.println(entry.getTitle().getPlainText());
            }
            writer.flush();

        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
}
